package com.ilink.groundservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ilink.groundservice.entity.PO.study.practice.StudyPractice;
import com.ilink.groundservice.entity.PO.study.practice.StudyPracticeBook;
import com.ilink.groundservice.service.study.StudyPracticeService;
import com.ilink.ilinkcommon.domain.R;
import com.ilink.ilinkcommon.log.method.MethodLog;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/ground/study/practice")
@RequiredArgsConstructor
public class StudyPracticeController {

    private final StudyPracticeService studyPracticeService;

    @MethodLog(value = "返回实训常用标签")
    @GetMapping("/tags")
    public R getPracticeTags() {
        try {
            List<String> tags = studyPracticeService.getAllPracticeTags();
            return R.ok("标签列表查询成功", tags);
        } catch (Exception e) {
            log.error("获取标签列表失败：{}", e.getMessage());
            return R.error("标签列表查询失败：" + e.getMessage());
        }
    }

    @MethodLog(value = "实训卡片信息")
    @GetMapping("/list")
    public R listPractices(
        @RequestParam(required = false) String keywords,
        @RequestParam(required = false) String tag,
        @RequestParam(defaultValue = "1") int page,
        @RequestParam(defaultValue = "20") int size) {
        try {
            IPage<StudyPractice> practicePage = studyPracticeService.getPracticeByCondition(keywords, tag, page, size);
            return R.ok("实训项目列表查询成功", practicePage);
        } catch (Exception e) {
            log.error("查询实训项目列表失败：{}", e.getMessage());
            return R.error("实训项目列表查询失败：" + e.getMessage());
        }
    }

    @GetMapping("/book/{practiceId}")
    public R getPracticeBookByPracticeId(@PathVariable Long practiceId){
        try {
            List<StudyPracticeBook> pages = studyPracticeService.getPracticeBookByPracticeId(practiceId);
            return R.ok("实验指导书查询成功", pages);
        } catch (Exception e) {
            log.error("实验指导书查询失败：{}", e.getMessage());
            return R.error("实验指导书查询失败：" + e.getMessage());
        }
    }
}
